var getEditDialog;
var delTemplate;
var $editDialog
require(['grid', 'topBar', 'layout'], function () {

    $('#top').topBar({
        left: [{
            text: '查询',
            type: 'btn',
            click: function () {
                search();
            }
        }
        ],
        right: [
            {
                text: '+新增模版',
                type: 'btn',
                authority: '',
                click: function () {
                    // 左侧树tab选中的类型
                    var type = commonFun.getLeftTreeCurrentTreeType();
                    var orgIds = commonFun.getLeftTreeCheckedNodes(type);
                    if(type != 'CAR'){
                        $.messager.alert("提示","请切换到车辆。","info");
                    }else if(!orgIds){
                        $.messager.alert("提示","请选择查询车辆。","info");
                    }else {
                        getEditDialog(0, '新增模版', '');
                    }
                }
            }
        ]
    });

    $('body').layout();

    $('#grid').grid({
        url: "/airConditioner/searchAirTemplatePage.htm",
        multiselect: false,
        colModel: [
            {name: "vrsnId", label: "vrsnId", key: true, hidden: true},
            {name: "vrsnName", label: "模版名称"},
            {name: "acSt", label: "A/C状态",
                formatter: function (cellvalue, options, rowObject) {
                    if (rowObject.acSt == "1") {
                        return " <em style='color: red'>开启</em>"
                    } else if (rowObject.acSt == "0") {
                        return " <em>关闭</em>"
                    }
                }
            },
            {name: "rcrdTept", label:"设定温度(°C)", align: "right"},
            {   name: "todo", label: "操作", width: 120, formatter: 'operArea',
                formatoptions: [
                    {
                        text: '查看',
                        css:'operate-scan',
                        click: function (data) {
                            getEditDialog(1, '查看', data.vrsnId);
                        }
                    },
                    {
                        text: '删除',
                        css:'operate-delete',
                        rule: function (data) {
                            return data.beenApplied == "0"
                        },
                        click: function (data) {
                            delTemplate(data.vrsnId);
                        }
                    }]
            }]
    });

    getEditDialog=function (edit, title, vrsnId) {
        if(!$editDialog){
            $editDialog = $('<div>').dialog({
                url: './mainte/setting/TemplateAirConditioEdit_jl.htm',
                height: 480,
                width: 720,
                close:function(){
                    $editDialog.dialog('exec', 'reset',true);
                },
                buttons: [
                    {
                        text: '下发',
                        css: 'mr10',
                        click: function () {
                            $(this).dialog('exec', 'issueParameters',function(){
                                $editDialog.dialog('close');
                                $.openTab('486af666d07f409aa4a6231ef3ccc0eb', 'autoSearch');
                            });
                        }
                    },
                    {
                        text: '关闭',
                        css: 'btn-cancel',
                        click: function () {
                            $(this).dialog('close');
                        }
                    }
                ]
            });
        }

        $editDialog.dialog('setOption', 'title',title);

        //添加
        if (edit == 0) {
            $editDialog.dialog('setOption', 'showBtn', [0, 1]);
        }
        //查看
        else if (edit == 1) {
            $editDialog.dialog('setOption', 'showBtn', [1]);
            reloadTemplate(vrsnId);
        }
        $editDialog.dialog('open');
    }

    //重新加载空调模板
    function reloadTemplate(vrsnId) {
        $editDialog.dialog('exec', 'reload', vrsnId);
    }

    //删除
    delTemplate=function(vrsnId) {
        $.messager.confirm("是否删除", "是否删除该模板信息？", function (r) {
            if (r) {
                $.ajaxExt({
                    url: "/airConditioner/delAirTemplate.htm",
                    data: {'vrsnId': vrsnId}
                }).done(function (data) {
                    search();
                });
            }
        });
    }

    //查询
    function search() {
        $('#grid').grid('setGridParam', {page: 1, datatype: "json", postData: {vrsnType:"VERS0008"}}).trigger("reloadGrid");
    }

    //进入页面自动查询
    search();

});
